package com.ac.cv.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;

public class KeyWordsWidget {

	private static KeyWordsWidget instance;

	private int maxValue;

	private int currentValue;

	private Map<String, List<KeyWord>> stringMap;

	private Map<Integer, List<KeyWord>> idMap;

	private KeyWordsWidget() {
		stringMap = new HashMap<String, List<KeyWord>>();
		idMap = new HashMap<Integer, List<KeyWord>>();
	}

	public static KeyWordsWidget getInstance() {
		if (instance == null) {
			instance = new KeyWordsWidget();
		}
		return instance;
	}

	public List<KeyWord> getKeyWord(String text) {
		return stringMap.get(text);
	}

	public List<KeyWord> getKeyWord(int id) {
		List<KeyWord> listKeyWord = idMap.get(Integer.valueOf(id));
		if(listKeyWord == null){
			listKeyWord = new ArrayList<KeyWord>();
		}
		return listKeyWord;
	}

	public void addKeyWord(KeyWord keyword) {
		
		List<KeyWord> keywordList = stringMap.get(keyword.getText());
		if (keywordList != null) {
			keywordList.add(keyword);
		} else {
			keywordList = new ArrayList<KeyWord>();
			keywordList.add(keyword);
			stringMap.put(keyword.getText(), keywordList);
		}
		if (keyword.getId() != -1) {
			List<KeyWord> keywordIdList = idMap.get(keyword.getId());
			if (keywordIdList != null) {
				keywordIdList.add(keyword);
			} else {
				keywordIdList = new ArrayList<KeyWord>();
				keywordIdList.add(keyword);
				idMap.put(Integer.valueOf(keyword.getId()), keywordIdList);
			}
		}
	}

	public Panel buildKeyWordsSearch() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStyleName("keyword");
		final KeyWordIteratorWidget nextButton = new KeyWordIteratorWidget("resources/images/arrow-right.gif");
		final KeyWordIteratorWidget precButton = new KeyWordIteratorWidget("resources/images/arrow-left.gif");
		panel.setSpacing(5);
		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
		for (String word : stringMap.keySet()) {
			oracle.add(word);
		}
		SuggestBox suggestBox = new SuggestBox(oracle);
		suggestBox.setAnimationEnabled(true);
		suggestBox.setLimit(5);
		suggestBox.addStyleName("keywordSearch");
		suggestBox.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				nextButton.setVisible(false);
				precButton.setVisible(false);
			}

		});
		suggestBox.addSelectionHandler(new SelectionHandler<Suggestion>() {

			public void onSelection(SelectionEvent<Suggestion> event) {
				final List<KeyWord> labelList = getKeyWord(event
						.getSelectedItem().getReplacementString());
				nextButton.resetList();
				nextButton.updateList(labelList);
				precButton.resetList();
				precButton.updateList(labelList);
				if (labelList != null && labelList.size() > 0) {
					labelList.get(0).onSelect();
					if (labelList.size() > 1) {
						KeyWordsWidget.this.maxValue = labelList.size() - 1;
						KeyWordsWidget.this.currentValue = 0;
						nextButton.setVisible(true);
						precButton.setVisible(true);
						nextButton.addClickHandler(new ClickHandler() {

							@Override
							public void onClick(ClickEvent event) {
								if (KeyWordsWidget.this.currentValue < KeyWordsWidget.this.maxValue) {
									KeyWordsWidget.this.currentValue++;
								} else {
									KeyWordsWidget.this.currentValue = 0;
								}
								nextButton.getKeyWordList().get(
										KeyWordsWidget.this.currentValue)
										.onSelect();
							}

						});
						precButton.addClickHandler(new ClickHandler() {

							@Override
							public void onClick(ClickEvent event) {
								if (KeyWordsWidget.this.currentValue > 0) {
									KeyWordsWidget.this.currentValue--;
								} else {
									KeyWordsWidget.this.currentValue = KeyWordsWidget.this.maxValue;
								}
								precButton.getKeyWordList().get(
										KeyWordsWidget.this.currentValue)
										.onSelect();
							}

						});
					} else {
						nextButton.setVisible(false);
						precButton.setVisible(false);
					}
				} else {
					nextButton.setVisible(false);
					precButton.setVisible(false);
				}
			}

		});
		Label texte = new Label();
		texte.setText("Recherche de mot-clés :");
		texte.addStyleName("text");
		panel.add(texte);
		panel.add(suggestBox);
		panel.add(precButton);
		panel.add(nextButton);
		nextButton.setVisible(false);
		precButton.setVisible(false);
		return panel;
	}

	private class KeyWordIteratorWidget extends Image {

		private List<KeyWord> keyWordList;

		public KeyWordIteratorWidget(String path) {
			super(path);
			this.setTitle("Parcourir le mot-clé dans le document");
			keyWordList = new ArrayList<KeyWord>();
		}

		public void resetList() {
			keyWordList.clear();
		}

		public void updateList(List<KeyWord> keyWordList) {
			if (keyWordList == null) {
				this.keyWordList = new ArrayList<KeyWord>();
			}
			this.keyWordList.addAll(keyWordList);
		}

		public List<KeyWord> getKeyWordList() {
			return keyWordList;
		}

	}

}
